package org.jash.sportnews2109.vm

import androidx.lifecycle.MutableLiveData
import org.jash.common.mvvm.BaseViewModel
import org.jash.common.retrofit
import org.jash.sportnews2109.net.APIService

class RegisterViewModel:BaseViewModel() {
    val service by lazy { retrofit.create(APIService::class.java)}
    val checkLiveData by lazy { MutableLiveData<String>() }
    val verifyLiveData by lazy { MutableLiveData<String>() }
    val registerLiveData by lazy { MutableLiveData<String>() }
    fun checkVerifyCode(map:Map<String, String>) {
        progress(service.checkRegisterCode(map)) {
            if (it.code == 0) {
                checkLiveData.postValue("校验通过")
            } else {
                errorLiveData.postValue(RuntimeException(it.msg))
            }
        }
    }
    fun getVerifyCode(phone:String) {
        progress(service.sendRegisterCode(phone)) {
            if (it.code == 0) {
                verifyLiveData.postValue(it.data)
            } else {
                errorLiveData.postValue(RuntimeException(it.msg))
            }
        }
    }
    fun register(user:Map<String, String>) {
        progress(service.register(user)) {
            if (it.code == 0) {
                verifyLiveData.postValue("注册成功")
            } else {
                errorLiveData.postValue(RuntimeException(it.msg))
            }
        }
    }
}